const getIncoming = (id, data) => {
    return data.filter(item => item.targetRef === id).map(items => items.id)
  }
  const getOutGoing = (id, data) => {
    return data.filter(item => item.sourceRef === id).map(items => items.id)
  }
  const getLabel = (data, labelStyle) => {
    const keyWord = ['isBold', 'isItalic', 'isStrikeThrough', 'isUnderline', 'fontFamily', 'size']
    const arr = data.filter(item => {
      return keyWord.find(key => {
        return key in labelStyle
      })
    })
    return arr.map(item => {
      const obj = {}
      keyWord.forEach(key => {
        if (labelStyle[key]) {
          obj[key === 'fontFamily' ? 'name' : key] = labelStyle[key] || ''
        }
      })
      return {
        '-id': item.id,
        'omgdc:Font': obj
      }
    })
  }
  export function convertJsonToBpmn(jsonData) {
    if (!jsonData || !Object.keys(jsonData).length) return {}
    const result = {
      definitions: {
        '-xmlns': 'http://www.omg.org/spec/BPMN/20100524/MODEL',
        '-xmlns:bpmndi': 'http://www.omg.org/spec/BPMN/20100524/DI',
        '-xmlns:omgdi': 'http://www.omg.org/spec/DD/20100524/DI',
        '-xmlns:omgdc': 'http://www.omg.org/spec/DD/20100524/DC',
        '-xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
        '-xmlns:bioc': 'http://bpmn.io/schema/bpmn/biocolor/1.0',
        '-xmlns:color': 'http://www.omg.org/spec/BPMN/non-normative/color/1.0',
        '-id': 'sid-38422fae-e03e-43a3-bef4-bd33b32041b2',
        '-targetNamespace': 'http://bpmn.io/bpmn',
        '-exporter': 'bpmn-js (https://demo.bpmn.io)',
        '-exporterVersion': '5.1.2',
        process: {
          '-id': 'Process_1',
          '-isExecutable': 'true',
          task: [],
          sequenceFlow: []
        },
        'bpmndi:BPMNDiagram': {
          '-id': 'BpmnDiagram_1',
          'bpmndi:BPMNPlane': {
            '-id': 'BpmnPlane_1',
            '-bpmnElement': 'Process_1',
            'bpmndi:BPMNShape': [],
            'bpmndi:BPMNEdge': []
          }
        },
        'bpmndi:BPMNLabelStyle': {}
      }
    }
  
    // Convert tasks
    jsonData.nodeLists.forEach(task => {
      const taskId = task.config.id
      const incoming = getIncoming(taskId, jsonData.lines)
      const outGoing = getOutGoing(taskId, jsonData.lines)
      const obj = {
        '-id': taskId
      }
      if (incoming.length > 1) {
        obj.incoming = incoming
      } else if (incoming.length === 1) {
        obj.incoming = incoming[0]
      }
      if (outGoing.length > 1) {
        obj.outgoing = outGoing
      } else if (outGoing.length === 1) {
        obj.outgoing = outGoing[0]
      }
  
      result.definitions.process.task.push(obj)
      const { x, y, width, height, labelStyle } = task.config
  
      const element = {
        '-id': `${taskId}_di`,
        '-bpmnElement': taskId,
        'omgdc:Bounds': {
          '-x': x,
          '-y': y,
          '-width': width,
          '-height': height
        },
        'bpmndi:BPMNLabel': {}
      }
      if (labelStyle && Object.keys(labelStyle).length) {
        const { x, y, width, height, id } = labelStyle
        element['bpmndi:BPMNLabel']['-labelStyle'] = id
        element['bpmndi:BPMNLabel']['omgdc:Bounds'] = {
          '-x': x,
          '-y': y,
          '-width': width,
          '-height': height
        }
        result.definitions['bpmndi:BPMNLabelStyle'] = getLabel(jsonData.nodeLists, labelStyle)
      }
      // Convert BPMN shapes
      result.definitions['bpmndi:BPMNDiagram']['bpmndi:BPMNPlane']['bpmndi:BPMNShape'].push(element)
    })
  
    // Convert sequence flows
    jsonData.lines.forEach(line => {
      const sequenceFlowId = line.id
      const sourceRef = line.sourceRef
      const targetRef = line.targetRef
  
      result.definitions.process.sequenceFlow.push({
        '-id': `${sequenceFlowId}`,
        '-name': line.name,
        '-sourceRef': sourceRef,
        '-targetRef': targetRef
      })
  
      // Convert BPMN edges
      result.definitions['bpmndi:BPMNDiagram']['bpmndi:BPMNPlane']['bpmndi:BPMNEdge'].push({
        '-id': `${sequenceFlowId}_di`,
        '-bpmnElement': sequenceFlowId,
        'omgdi:waypoint': line.point.map(p => {
          return { '-x': p.x, '-y': p.y }
        }),
        'bpmndi:BPMNLabel': {
          'omgdc:Bounds': {
            '-x': line.x,
            '-y': line.y,
            '-width': line.width,
            '-height': line.height
          }
        }
      })
    })
  
    return result
  }
  
  